package com.orangelife.service.propFeeCommInfo.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import com.orangelife.exception.AppBizException;
import com.orangelife.service.propFeeUnit.IPropFeeUnitService;
import com.orangelife.service.propFeeUnit.model.PropFeeUnitDto;
import com.orangelife.setting.constant.TypeConst;

public class HandleUnitThread extends Thread implements Runnable {

	private String threadName;
	private List<Map<String, Object>> data;
	private int start;
	private int end;

	private IPropFeeUnitService propFeeUnitService;

	public HandleUnitThread(String threadName, List<Map<String, Object>> data,
			IPropFeeUnitService propFeeUnitService) {
		this.threadName = threadName;
		this.data = data;
		this.propFeeUnitService = propFeeUnitService;
	}

	public void run() {
		try {

			/***
			 * 批量查询已经存在的楼栋
			 */
			List<String> unitIDs = new ArrayList<String>();
			List<PropFeeUnitDto> feeUnits = new ArrayList<PropFeeUnitDto>();
			for (Map<String, Object> unit : data) {
				if (!StringUtils.isBlank(unit.get("unitID").toString())) {
					unitIDs.add(unit.get("unitID").toString());
				}
			}
			if (unitIDs != null && unitIDs.size() > 0) {
				feeUnits = propFeeUnitService.bauthfindPropFeeUnit(unitIDs);
			}
			List<PropFeeUnitDto> unitDtos = new ArrayList<PropFeeUnitDto>();
			for (Map<String, Object> unit : data) {
				System.out.println(threadName + "\t\t\t\t\t 请求 数据" + "\t\t\t\t"
						+ JSONObject.fromObject(unit).toString());
				/****
				 * 判断单元是否存在
				 */
				PropFeeUnitDto feeUnit = null;
				for (PropFeeUnitDto propFeeUnitDto : feeUnits) {
					if (propFeeUnitDto.getUnitID().equals(
							unit.get("unitID").toString())) {
						feeUnit = propFeeUnitDto;
						break;
					}
				}

				/***
				 * 存在修改单元
				 */
				if (feeUnit != null) {
					feeUnit.setBuildingID(unit.get("buildingID").toString());
					feeUnit.setUnitName(unit.get("unitName").toString());
					//feeUnit.setTsedit(new Date());
					propFeeUnitService.editPropFeeUnit(feeUnit);
				} else {

					/***
					 * 不存在添加单元
					 */
					List<PropFeeUnitDto> unis = propFeeUnitService
							.findPropFeeUnitByRelevantID(unit.get("unitID")
									.toString(), unit.get("buildingID")
									.toString(), unit.get("unitName")
									.toString());
					if (unis == null || unis.size() <= 0) {
						PropFeeUnitDto propFeeUnit = new PropFeeUnitDto();
						propFeeUnit.setUnitID(unit.get("unitID").toString());
						propFeeUnit.setBuildingID(unit.get("buildingID")
								.toString());
						propFeeUnit
								.setUnitName(unit.get("unitName").toString());
						propFeeUnit.setItemStat(TypeConst.FLAG_ON);
						propFeeUnit.setTscreate(new Date());
						propFeeUnit.setTsedit(new Date());
						unitDtos.add(propFeeUnit);
					}
				}

			}
			propFeeUnitService.addPropFeeUnit(unitDtos);
		} catch (AppBizException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
